var app = angular.module('BaseControl');

app.directive("pagetoolbar", [function() {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
            module: '=?'
        },
        templateUrl: 		// 替换HTML (使用scope中的变量)
            'modules/Base/client/views/templates/PageToolbar.html',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function ($scope) {
            var keyCodes = [13, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
            $scope.keypress = function(){
                if(keyCodes.indexOf(event.keyCode) < 0){
                    event.returnValue = false;
                    return;
                }

                if(event.keyCode == 13) {
                    if(event.srcElement.value != '' && !isNaN(event.srcElement.value)) {
                        if(parseInt(event.srcElement.value) > 0) {
                            aassignPage(parseInt(event.srcElement.value) - 1);
                            event.srcElement.value = "";
                        }
                    }
                }
            };
            $scope.textBlur = function(){
                if(event.srcElement.value != '' && event.srcElement.value > 0){
                    aassignPage(event.srcElement.value - 1);
                    event.srcElement.value = '';
                }
            };

            $scope.firstPage = function(){
                aassignPage(0);
            };
            $scope.prePage = function(){
                aassignPage($scope.module.PageNum - 1);
            };
            $scope.nextPage = function(){
                aassignPage($scope.module.PageNum + 1);
            };
            $scope.lastPage = function(){
                aassignPage($scope.module.PageCount - 1);
            };
            $scope.pageNumClick = function(){
                aassignPage(parseInt(event.srcElement.innerText) - 1);
            };

            var aassignPage = function(pageIndex){
                if(pageIndex >= 0 && pageIndex < $scope.module.PageCount
                 && pageIndex != $scope.module.PageNum){
                    $scope.module.aassignPage(pageIndex);
                }
            };
        }],
        link: function (scope, element, attrs) {
            scope.DisplayPageCount = 5;
            var displayPages;

            var setDisplayPages = function(){
               displayPages = new Array();

               if(scope.module != null){
                  element[0].children[5].innerText = "共" + scope.module.PageCount.toString() + "页";
                  if(scope.module.PageCount <= scope.DisplayPageCount){
                     for(var i=0; i<scope.module.PageCount; i++){
                         displayPages.push(i);
                     }
                  }
                  else{
                     if(scope.module.PageNum + 1 <= Math.ceil(scope.DisplayPageCount / 2)){
                         for(var i=0; i<scope.DisplayPageCount; i++) {
                             displayPages.push(i);
                         }
                     }
                     else if(scope.module.PageCount - (scope.module.PageNum + 1) < Math.ceil(scope.DisplayPageCount / 2)){
                         for(var i=scope.module.PageCount - scope.DisplayPageCount; i<scope.module.PageCount; i++){
                             displayPages.push(i);
                         }
                     }
                     else{
                         var firstPageNum = scope.module.PageNum - Math.floor(scope.DisplayPageCount / 2);
                         for(var i=0; i<scope.DisplayPageCount; i++){
                             displayPages.push(firstPageNum + i);
                         }
                     }
                  }
               }

               for(var i=0; i<displayPages.length; i++){
                    if(displayPages[i] == scope.module.PageNum){
                        element[0].children[2].children[i].setAttribute("class", "PTNumSeleted");
                    }
                    else{
                        element[0].children[2].children[i].setAttribute("class", "PTNumNoSeleted");
                    }
                    element[0].children[2].children[i].style.display = '';
                    element[0].children[2].children[i].innerText = (displayPages[i] + 1).toString();
               }

               if(displayPages.length < scope.DisplayPageCount){
                    for(var i=displayPages.length; i<scope.DisplayPageCount; i++){
                        element[0].children[2].children[i].style.display = 'none';
                    }
               }
            };
            
            element[0].children[7].children[0].addEventListener('change', function(){
                if(event.srcElement.value < 1){
                    event.srcElement.value = 1;
                }
                else if(event.srcElement.value > scope.module.PageCount){
                    event.srcElement.value = scope.module.PageCount;
                }
            });

            if(scope.module != undefined && scope.module != null){
                setDisplayPages();
                scope.$on("PageToolbar" + scope.module._ID, function(event){
                    setDisplayPages();
                });
            }
        }
    }
}]);
